<?php

namespace common\ui\backend\widgets;

use common\ui\backend\AdminLteAsset;
use yii\base\Widget;
use yii\web\AssetManager;
use Yii;

/**
 *
 */
class SidebarWidget extends Widget
{
    /**
     * @var AssetManager
     */
    public $assetManager;
    public $title;
    public $userName;
    public $showLogo = false;
    public $showUser = false;
    public $menus = [
        [
            'menu' => [
                [
                    'icon' => 'fa-chart-pie',
                    'name' => 'A',
                    'url' => '',
                    'children' => [
                        [
                            'icon' => 'fa-circle',
                            'name' => 'B',
                            'url' => '',
                        ]
                    ]
                ]
            ]
        ]
    ];

    public function run()
    {
        $currUrl = '/' . Yii::$app->controller->id . '/' . Yii::$app->controller->action->id;
        foreach ($this->menus[0]['menu'] as $k => $v) {
            $v['active'] = false;
            if ($v['url'] == $currUrl || (Yii::$app->session->get('url') == $v['url'] && Yii::$app->session->get('url'))) {
                $v['active'] = true;
            }
            if (isset($v['children'])) {
                foreach ($v['children'] as $k1 => $v1) {
                    $v1['active'] = false;
                    if ($v1['url'] == $currUrl || (Yii::$app->session->get('url') && Yii::$app->session->get('url') == $v1['url'])) {
                        $v1['active'] = true;
                        $v['active'] = true;
                    }
                    $v['children'][$k1] = $v1;
                }
            }
            $this->menus[0]['menu'][$k] = $v;
        }
        echo $this->render('sidebar2', [
            'baseUrl' => $this->assetManager->getBundle(AdminLteAsset::class)->baseUrl . '/backend/',
            'title' => $this->title,
            'userName' => $this->userName,
            'menus' => $this->menus,
            'showLogo' => $this->showLogo,
            'showUser' => $this->showUser
        ]);
    }
}